#include<iostream>
#include<json/json.h>
#include<sstream>
#include<memory>


bool Serialize(Json::Value&  val,std::string& dst)
{
    Json::StreamWriterBuilder swb;
    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
    std::stringstream ss;
    if(sw->write(val,&ss)!=0)
    {
         std::cout<<"序列化失败\n";
         return false;
    }
    dst = ss.str();
    return true;
}
bool Deserialize(std::string& src,Json::Value& val)
{
    Json::CharReaderBuilder crb;
    std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
    std::string err;
    if(!cr->parse(src.c_str(),src.c_str()+src.size(),&val,&err))
    {
        std::cout<<"反序列化失败\n";
        return false;
    }
    return true;
}
int main()
{
    char name[] = "zzy";
    int age = 18;
    float score[3]={66.6,100.0,99.8};
    Json::Value stu;
    stu["姓名"] = name;
    stu["年龄"] = age;
    stu["成绩"].append(score[0]);
    stu["成绩"].append(score[1]);
    stu["成绩"].append(score[2]);
    std::string dst;
    if(!Serialize(stu,dst)) return -1;
    std::cout<<dst<<std::endl;
    Json::Value val;
    if(!Deserialize(dst,val)) return -1;
    std::cout<<val["姓名"]<<std::endl;
    std::cout<<val["年龄"]<<std::endl;
    size_t sz = val["成绩"].size();
    for(int i =0;i<sz;i++)
    std::cout<<val["成绩"][i].asFloat()<<std::endl;
}